package com.jiaai.web.pay;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.family.model.TmemberVO;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.pay.ITxApi;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import com.jiaai.util.code_pack.ErrorCode;
import com.jiaai.util.code_pack.TipsProperties;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.rmi.ServerException;

/**
 * class_name:ZTXController
 * describe:------------------>统一提现接口
 * create_user:张昉平
 * create_date:2018/2/24
 * create_time:12:40
 */
@RestController
@RequestMapping("/api/v1/app/tx")
public class ZTXController {

    @Reference
    private ITxApi txApi;


    @PostMapping("/manual")
    @TokenIntercept(checkVisitor = true)
    public Object manualTx(@RequestParam String ticket)throws ServerException{
        AccessUser accessUser=RequestUtils.getUser();
        try {
            txApi.manualTx(ticket,accessUser.getUserId());
            return  RestResultGenerator.genSuccessResult("提现成功");
        }catch (Exception e){
            return  RestResultGenerator.genErrorResult(e.getMessage());
        }
    }

    /**
     * 主播提现, 先生成订单, 待后台审核
     * @return
     */
    @PostMapping("")
    @TokenIntercept(checkVisitor = true)
    public Object tx(@RequestBody TxAppOrderVO appOrderVO){
        AccessUser user = RequestUtils.getUser();

        int res = txApi.toTx(appOrderVO.getMoney(), appOrderVO.getRmb(), appOrderVO.getType()
                , appOrderVO.getSubject(), user.getUserId(), "");
        switch (res){
            case 0:
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_ANCHOR, TipsProperties.getTip("NOT_ANCHOR"));
            case -1:
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_ENOUGH_AE, TipsProperties.getTip("NOT_ENOUGH_AE"));
            case -2:
                return RestResultGenerator.genErrorResult(ErrorCode.RATE_ERROR, TipsProperties.getTip("RATE_ERROR"));
            case -3:
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_BIND_WX, TipsProperties.getTip("NOT_BIND_WX"));
            case -4:
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_BIND_ALI, TipsProperties.getTip("NOT_BIND_ALI"));
            case -5:
                return RestResultGenerator.genErrorResult(ErrorCode.LESS_MIN_MONEY, TipsProperties.getTip("LESS_MIN_MONEY"));
            case 1:
                return RestResultGenerator.genSuccessResult("提现订单提交成功, 请耐心等待1到2个工作日");
            default:
                return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    public static class TxAppOrderVO{
        /**
         * * @param type 渠道 1:微信 2:支付宝
         * @param money 平台币
         */
        private BigDecimal money;
        private Integer type;
        private String subject;
        private BigDecimal rmb;//兑换后的人民币

        public BigDecimal getRmb() {
            return rmb;
        }

        public void setRmb(BigDecimal rmb) {
            this.rmb = rmb;
        }

        public BigDecimal getMoney() {
            return money;
        }

        public void setMoney(BigDecimal money) {
            this.money = money;
        }

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }
    }

    @GetMapping("/myTxOrder")
    @TokenIntercept
    public Object myTxOrder(Integer curr) throws Exception {
        AccessUser accessUser= RequestUtils.getUser();
        return RestResultGenerator.genSuccessResult(txApi.myTxOrder(curr,accessUser.getUserId()));
    }

}
